<?php
//Zend_Loader::loadFile('../application/admin/models/Template.php');
class Newsletter 
{
    /**
     * @var object Zend DB Adapter
     */
    private $_db;
    /**
     * @var string
     */
   
    public function __construct()
    {
			$this->_db = Zend_Registry::get('db');
    }
    
    public function listNewsletters()
    {
    	$getNewsletters = $this->_db->fetchAll("
			SELECT n.newsletter_id, n.newsletter_title, n.newsletter_content, n.template_id, t.template_title
			FROM newsletters as n
			LEFT JOIN templates as t ON t.template_id = n.template_id
		");
		
		return $getNewsletters;
	}
	

    public function getNewsletter($newsletter_id)
    {
    //	echo $newsletter_id;exit;
    	$getNewsletterdetails = $this->_db->fetchrow("
			SELECT newsletter_id, newsletter_title, newsletter_content, template_id
			FROM  newsletters 
			WHERE  newsletter_id='".$newsletter_id."'
			
		");
		//print_r($getNewsletterdetails);
		return $getNewsletterdetails;
       
    }

    public function addNewsletter($form_values)
    {
        $template_id = $form_values['template_id'];
        $newsletter_title = $form_values['newsletter_title'];
        $newsletter_content = $form_values['newsletter_content'];
         
        $addNewsletter = $this->_db->exec("
                INSERT INTO newsletters (newsletter_title,newsletter_content,template_id,status) 
                VALUES ('" . $newsletter_title . "','" . $newsletter_content . "','" . $template_id . "', 1)
	    ");
        
        if($addNewsletter)
        {
            echo "Newsletter has been successfully created";
        }
    }

    public function updateNewsletter($form_values)
    {
        $template_id = $form_values['template_id'];
        $newsletter_title = $form_values['newsletter_title'];
        $newsletter_content = $form_values['newsletter_content'];
        $newsletter_id = $form_values['newsletter_id'];
        
        $update_newsletter = $this->_db->exec("
            UPDATE  newsletters 
            SET newsletter_title='" . $newsletter_title . "' ,newsletter_content='" . $newsletter_content . "', template_id='" . $template_id. "' 
            WHERE  newsletter_id=" . $newsletter_id
        );
        
        if($update_newsletter)
        {
            echo "Selected newsletter details has been successfully updated";            
        }
    }

    public function deleteNEwsletter($newsletter_id)
    {
    	$sql = "DELETE FROM newsletters 
    			WHERE newsletter_id = " . $newsletter_id;
    	$delete_newsletter = $this->_db->exec($sql);
    	
    	if($delete_newsletter)
    	{
    		echo "Selected newsletter has been successfully deleted";
    	}
	}

}

